<?php
namespace App\Http\Controllers\Merchant;
use App\Http\Controllers\BaseController;
use App\Models\AdvInvite;
use App\Models\AdvSignUp;
use App\Models\Balance;
use App\Models\ProducerAdv;
use App\Models\ProducerAdvSnapshot;
use Illuminate\Http\Request;

class SignUpController extends BaseController
{

    public function store(Request $request) {
        $input = $request->input();
        $this->validate($request, [
            'adv_id'  => 'required',
        ]);
        $input['merchant_id'] = $this->getUserId();
        $adv_id = $request->input('adv_id');

        $adv = ProducerAdv::find($adv_id);
        if (!$adv)
            return $this->failed('任务不存在');

        $exists = AdvSignUp::where('merchant_id', $input['merchant_id'])
            ->where('adv_id', $adv_id)->exists();
        if ($exists)
            return $this->failed('已预约过');

        $arr = $adv->toArray();
        unset($arr['id']);

        try {
            return \DB::transaction(function () use($arr, $adv, $adv_id){
                $snapshot = new ProducerAdvSnapshot();
                foreach ($arr as $key => $value) {
                    $snapshot-> {$key} = $value;
                }
                $user_id = $this->getUserId();
                $snapshot->save();
                $input['adv_snapshot_id'] = $snapshot->id;
                $input['order_sn'] = create_order_no('s');
                $input['producer_id'] = $adv->merchant_id;
                $input['merchant_id'] = $user_id;
                $input['adv_id'] = $adv_id;
                $model = AdvSignUp::create($input);
                $adv->increment('sign_up_count', 1);
                AdvInvite::where('daren_id', $input['merchant_id'])->where('adv_id', $adv_id)->update(['status'=>1]);
                return $this->success(['id' => $model->id], '预约成功');
            });
        } catch (\Exception $e) {
            \Log::error($e);
            return $this->failed($e->getMessage());
        }
    }


    public function index(Request $request) {
        $input = $request->input();
        $search = ['status' => -1, 'start_at' => '', 'end_at' => ''];
        $daren_id = $this->getUserId();
        $input['type'] = 'my';
        $title = $request->input('title');
        $adv_sn = $request->input('adv_sn');
        $list = AdvSignUp::when($title, function ($q) use($title){
            $q->whereHas('adv', function ($qt) use($title){
                $qt->where('title', 'like', '%' . $title . '%');
            });
        })->when($adv_sn, function ($q) use($adv_sn){
            $q->whereHas('adv', function ($qt) use($adv_sn){
                $qt->where('adv_sn', 'like', '%' . $adv_sn . '%');
            });
        })->apply($search, $input)->when($input['type'] == 'my', function ($q) use($daren_id){
            $q->where('merchant_id', $daren_id);
        })->withCertain('adv.merchant', ['fac_title'])->orderBy('id', 'desc')->paginate(per_page());
        return $this->paginate($list);
    }

    public function order_info(Request $request) {
        $this->validate($request, [
            'order_id' => 'required'
        ]);
        $order_id = $request->input('order_id');
        $info = AdvSignUp::withCertain('adv.merchant', ['fac_title'])->find($order_id);
        return $this->success($info);
    }

    public function remark(Request $request) {
        $this->validate($request, [
            'order_id' => 'required',
            'remark' => 'required',
        ]);
        $order_id = $request->input('order_id');
        $model = AdvSignUp::find($order_id);
        $model->remark = $request->input('remark');
        $model->save();
        return $this->success(['id' => $order_id], '备注成功');
    }

    public function url(Request $request) {
        $this->validate($request, [
            'order_id' => 'required',
            'url' => 'required',
        ]);
        $order_id = $request->input('order_id');
        $model = AdvSignUp::find($order_id);
        $model->url = $request->input('url');
        $model->save();
        return $this->success(['id' => $order_id], '操作成功');
    }

    public function adv_flow() {
        $user_id = $this->getUserId();
        $list = Balance::where('type', Balance::TYPE_ADV)
            ->withCertain('adv_sign_up.adv', ['title'])
            ->where('merchant_id', $user_id)
            ->orderBy('id', 'desc')->paginate(per_page());

        $account = Balance::where('type', Balance::TYPE_ADV)->where('merchant_id', $user_id)->sum('amount');
        $balance = format_yuan($account);
        return $this->paginate($list, compact('balance'));
    }

}

